package com.neusoft.homework1.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.neusoft.homework1.R;
import com.neusoft.homework1.beans.Dish;
import com.neusoft.homework1.retrofitservice.RetrofitService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    private EditText username,password;
    private Button loginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        loginButton = findViewById(R.id.button5);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //启动界面

                /*Intent intent = new Intent();
                intent.setClass(MainActivity.this,HomeActivity.class);
                startActivity(intent);
                finish();*/


                //1
                Retrofit retrofit =  new Retrofit.Builder()
                        .baseUrl("80/MenuAPIDemo/ http://172.17.7.95:8080/MenuAPIDemo/")
                        .addConverterFactory(GsonConverterFactory. create ())
                        .build();

                //2
                RetrofitService retrofitService =
                        retrofit.create(RetrofitService.class);

                //3
                Call<List<Dish>>dishesCall
                         = retrofitService.getDishes();

                //4
                dishesCall.enqueue(new Callback<List<Dish>>() {
                    @Override
                    public void onResponse(Call<List<Dish>> call,
                                           Response<List<Dish>> response) {

                        List<Dish> dishes = response.body();
                        Toast.makeText(MainActivity.this, "dish"+dishes.size(),
                                Toast.LENGTH_SHORT).show();



                    }






                    @Override
                    public void onFailure(Call<List<Dish>> call, Throwable t)
                    {

                        Toast.makeText(MainActivity.this, t.getLocalizedMessage(),
                                Toast.LENGTH_SHORT).show();



                    }
                });

            }
        });
    }
}


















